<template>
  <div class="app-container">
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="关 键 字">
              <el-input
                style="width: 95%"
                v-model="searchObj.keyword"
                placeholder="用户名/姓名/手机号码"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="fetchData()"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetData()"
            >重置</el-button
          >
        </el-row>
        <el-row style="display: flex"> </el-row>
      </el-form>
    </div>

    <hr />
    <!-- 工具栏 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add"
        >添 加</el-button
      >
      <el-button class="btn-add" size="mini" @click="batchRemove()"
        >批量删除</el-button
      >
    </div>
    <!-- 列表 -->
    <el-table
      :data="list"
      stripe
      border
      style="width: 100%; margin-top: 10px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" />
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="id" label="员工ID"  />
      <el-table-column prop="empname" label="员工姓名"/>
      <el-table-column prop="empphone" label="手机号"/>
      <el-table-column prop="empemail" label="邮箱"/>
      <el-table-column prop="emppassword" label="密码"/>

      <el-table-column label="操作" align="center" fixed="right" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit(scope.row.id)"
            title="修改"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeDataById(scope.row.id)"
            title="删除"
          />
          <el-button
            type="warning"
            icon="el-icon-baseball"
            size="mini"
            @click="showAssignAuth(scope.row)"
            title="查看账户"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

    <!-- 弹框 -->
    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form
        ref="dataForm"
        :model="sysUser"
        label-width="100px"
        size="small"
        style="padding-right: 40px"
      >
        <el-form-item label="员工id" prop="id">
          <el-input v-model="sysUser.id" />
        </el-form-item>
        <el-form-item label="员工姓名" prop="empname">
          <el-input v-model="sysUser.empname" />
        </el-form-item>
        <el-form-item label="手机号" prop="empphone">
          <el-input v-model="sysUser.empphone" />
        </el-form-item>
        <el-form-item label="密码" prop="emppassword">
          <el-input v-model="sysUser.emppassword" />
        </el-form-item>
        <el-form-item label="email" prop="email">
          <el-input v-model="sysUser.empemail" />
        </el-form-item>
        
        
        

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button
          @click="dialogVisible = false"
          size="small"
          icon="el-icon-refresh-right"
          >取 消</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-check"
          @click="saveOrUpdate()"
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from "@/api/emp/emp.js";

const defaultFrom = {
  id: "",
  empname: "",
  emppassword:'',
  empphone:'',
  empemail:''
  
};
export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: [], // 数据列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 5, // 每页记录数
      searchObj: {}, // 查询表单对象

      dialogVisible: false,
      sysUser: {},
      saveBtnDisabled: false,

      dialogRoleVisible: false,
      selectValueData: [],
    };
  },
  created() {
    this.searchObj = {};
    this.fetchData();
  },
  methods: {
    showAssignAuth(row) {
      this.$router.push(
        "/system/userSystem/account?id=" + row.id + "&realname=" + row.realname
      );
    },
    // 重置表单
    resetData() {
      this.searchObj = {};
      this.fetchData();
    },
    fetchData(page = 1) {
      this.page = page;
      if (this.createTimes && this.createTimes.length == 2) {
        this.searchObj.createTimeBegin = this.createTimes[0];
        this.searchObj.createTimeEnd = this.createTimes[1];
      }

      api
        .getEmpInfoList(this.page, this.limit, this.searchObj)
        .then((response) => {
          //this.list = response.data.list
          this.list = response.data.records;
          console.log(response.data.records);
          this.total = response.data.total;
          // 数据加载并绑定成功
          this.listLoading = false;
        });
    },
    handleSelectionChange(selectValue) {
      // console.log(selectValue);
      this.selectValueData = selectValue;
    },

    add() {
      this.dialogVisible = true;
      this.sysUser = {};
    },
    edit(id) {
      this.dialogVisible = true;
      api.findUserById(id).then((response) => {
        this.sysUser = response.data;
      });
    },
    saveOrUpdate() {
      if (this.sysUser.id != null) {
        this.updateUser();
      } else {
        this.addUser();
      }
    },
    addUser() {
      api.addUser(this.sysUser).then(() => {
        this.$message({
          message: "添加用户成功",
          type: "success",
        }),
          (this.dialogVisible = false);
        this.fetchData();
      });
    },
    updateUser() {
      api.updateUser(this.sysUser).then(() => {
        this.$message({
          message: "修改用户成功",
          type: "success",
        });
        this.dialogVisible = false;
        this.fetchData();
      });
    },
    removeDataById(id) {
      console.log("id" + id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        api.deleteUserById(id).then((response) => {
          // 提示
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 刷新页面
          this.fetchData();
        });
      });
    },
  },
};
</script>

<style></style>
